package com.netdisk.service.impl;
import com.netdisk.dto.request.user.InfoDTO;
import com.netdisk.dto.request.user.LoginDTO;
import com.netdisk.dto.request.user.RegisterDTO;
import com.netdisk.dto.response.user.DetailedInfoDTO;
import com.netdisk.dto.response.user.StorageInfoDTO;
import com.netdisk.dto.response.user.UserDTO;
import com.netdisk.entity.User;
import com.netdisk.exception.BusinessException;
import com.netdisk.mapper.UserMapper;
import com.netdisk.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    /**
     * 默认存储配额（1GB）
     */
    private static final long DEFAULT_STORAGE_QUOTA = 1024 * 1024 * 1024;

    /**
     * 用户登录
     */
    @Override
    public UserDTO login(LoginDTO loginDTO){

        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();

        //查询用户信息
        User user = userMapper.findByUsername(username);
        if(user == null){
            throw new BusinessException("用户不存在");
        }

        //先加密再校验密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(user.getPassword())){
            throw new BusinessException("密码错误");
        }

        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        //返回用户信息
        return userDTO;
    }

    /**
     * 用户注册
     */
    @Override
    public UserDTO register(RegisterDTO registerDTO){
        String username = registerDTO.getUsername();
        if(userMapper.findByUsername(username) != null){
            throw new BusinessException("用户名已存在");
        }

        String password = registerDTO.getPassword();
        String confirmPassword = registerDTO.getConfirmPassword();
        if(!password.equals(confirmPassword)){
            throw new BusinessException("密码不一致");
        }

        String email = registerDTO.getEmail();
        if(userMapper.findByEmail(email) != null){
            throw new BusinessException("电子邮箱已存在");
        }

        // 密码加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 创建用户
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setStorageQuota(DEFAULT_STORAGE_QUOTA);
        user.setStorageUsed(0L);

        // 保存用户
        userMapper.insert(user);

        // 返回用户信息
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        return userDTO;
    }

    /**
     * 获取用户信息
     */
    @Override
    public DetailedInfoDTO getDetailedInfo (InfoDTO infoDTO){
        User user = userMapper.findById(infoDTO.getId());
        if (user != null){
            DetailedInfoDTO detailedInfoDTO = new DetailedInfoDTO();
            BeanUtils.copyProperties(user, detailedInfoDTO);
            return detailedInfoDTO;
        }
        //真实场景场景中不会出现错误，这里只是为了演示
        else{
            throw new BusinessException("错误的id");
        }

    }

    /**
     * 获取用户存储空间使用统计信息
     */
    @Override
    public StorageInfoDTO getStorageInfo(InfoDTO infoDTO){
        User user = userMapper.findById(infoDTO.getId());
        if (user != null){
            StorageInfoDTO storageInfoDTO = new StorageInfoDTO();
            BeanUtils.copyProperties(user, storageInfoDTO);
            return storageInfoDTO;
        }
        //真实场景场景中不会出现错误，这里只是为了演示
        else{
            throw new BusinessException("错误的id");
        }
    }
}
